<template>
  <div>
    <a-card :bordered="false" :bodyStyle="tstyle">
      <div class="table-page-search-wrapper" v-if="hasPerm('appVipOrder:page')">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="4" :sm="24">
              <a-form-item label="订单号">
                <a-input v-model="queryParam.orderNo" allow-clear placeholder="请输入订单号"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="用户昵称">
                <a-input v-model="queryParam.nick" allow-clear placeholder="用户昵称"/>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="用户手机">
                <a-input v-model="queryParam.loginName" allow-clear placeholder="用户手机"/>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="4" :sm="24">
                <a-form-item label="支付方式">
                  <a-select v-model="queryParam.payType" alow-clear placeholder="请选择支付方式" >
                    <a-select-option v-for="(item,index) in payTypeDict" :key="index" :value="item.code" >{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="支付状态">
                  <a-select v-model="queryParam.status" alow-clear placeholder="请选择支付状态" >
                    <a-select-option v-for="(item,index) in payStatusDict" :key="index" :value="item.code" >{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="退款状态">
                  <a-select v-model="queryParam.refundStatus" alow-clear placeholder="请选择支付状态" >
                    <a-select-option v-for="(item,index) in refundStatusDict" :key="index" :value="item.code" >{{ item.name }}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="套餐类型">
                  <a-select v-model="queryParam.vipPackageId" alow-clear placeholder="套餐类型">
                    <a-select-option v-for="(item,index) in vipPackageDict" :key="index" :value="item.code">{{item.name }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="4" :sm="24">
                <a-form-item label="商户号">
                  <a-input v-model="queryParam.mchName" allow-clear placeholder="商户号"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="订单时间">
                  <a-range-picker
                    v-model="queryParam.dates"
                    :show-time="{
                      hideDisabledOptions: true,
                      defaultValue: [moment('00:00:00', 'HH:mm:ss'), moment('23:59:59', 'HH:mm:ss')],
                    }"
                    format="YYYY-MM-DD HH:mm:ss"
                  />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="4" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </a-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :rowKey="(record) => record.id"
      >
        <span slot="status" slot-scope="text">
          {{text=='0'?'已支付': ''}}
        </span>
        <span slot="refundStatus" slot-scope="text">
          {{text=='0'?'已退款': ''}}
        </span>
        <span slot="payType" slot-scope="text">
          {{ 'pay_type' | dictType(text) }}
        </span>
        <span slot="vipPackageId" slot-scope="text">
          {{ 'vip_package' | dictType(text) }}
        </span>
        <span slot="mobileSpec" slot-scope="text">
          <div v-if="text.includes('iPhone')" class="zy-text-line" style="color: #fb5059;">苹果</div>
          <div v-else class="zy-text-line" style="color: #006EFF">安卓</div>
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('appUser:refund')" @click="refund(record)">退款</a>
        </span>
      </s-table>
    </a-card>
  </div>
</template>
<script>
  import { STable, XDown } from '@/components'
  import moment from 'moment'
  import { appVipOrderPage, appVipOrderRefund } from '@/api/modular/app/vip/appVipOrderManage'
  export default {
    components: {
      STable,
      XDown
    },
    data() {
      return {
        // 高级搜索 展开/关闭
        advanced: true,
        // 查询参数
        queryParam: {},
        // 表头
        columns: [
          {
            title: '订单号',
            align: 'center',
            dataIndex: 'orderNo'
          },
          {
            title: '用户昵称',
            align: 'center',
            dataIndex: 'nick'
          },
          {
            title: '用户手机',
            align: 'center',
            dataIndex: 'loginName'
          },
          {
            title: '金额',
            align: 'center',
            dataIndex: 'money'
          },
          {
            title: '支付状态',
            align: 'center',
            dataIndex: 'status',
            scopedSlots: { customRender: 'status' }
          },
          {
            title: '支付方式',
            align: 'center',
            dataIndex: 'payType',
            scopedSlots: { customRender: 'payType' }
          },
          {
            title: '设备型号',
            align: 'center',
            dataIndex: 'mobileSpec',
            scopedSlots: { customRender: 'mobileSpec' }
          },
          {
            title: '商户号',
            align: 'center',
            dataIndex: 'mchName'
          },
          {
            title: '创建时间',
            align: 'center',
            dataIndex: 'createTime'
          },
          {
            title: '退款状态',
            align: 'center',
            dataIndex: 'refundStatus',
            scopedSlots: { customRender: 'refundStatus' }
          },
          {
            title: '退款时间',
            align: 'center',
            dataIndex: 'refundTime',
            scopedSlots: { customRender: 'refundTime' }
          }
          // {
          //   title: '套餐种类',
          //   align: 'center',
          //   dataIndex: 'vipPackageId',
          //   scopedSlots: { customRender: 'vipPackageId' }
          // }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return appVipOrderPage(Object.assign(parameter, this.switchingDate())).then((res) => {
            return res.data
          })
        },
        payStatusDict: [],
        refundStatusDict: [],
        payTypeDict: [],
        vipPackageDict: []
      }
    },
    created() {
      this.sysDictTypeDropDown()
      this.columns.push({
        title: '操作',
        fixed: 'right',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    },
    methods: {
      moment,
      /**
       * 查询参数组装
       */
      switchingDate () {
        const dates = this.queryParam.dates
        if (dates != null) {
          this.queryParam.searchBeginTime = moment(dates[0]).format('YYYY-MM-DD HH:mm:ss')
          this.queryParam.searchEndTime = moment(dates[1]).format('YYYY-MM-DD HH:mm:ss')
          if (dates.length < 1) {
            delete this.queryParam.searchBeginTime
            delete this.queryParam.searchEndTime
          }
        }
        const obj = JSON.parse(JSON.stringify(this.queryParam))
        delete obj.dates
        return obj
      },
      /**
       * 获取字典数据
       */
      sysDictTypeDropDown () {
        this.payStatusDict = this.$options.filters['dictData']('pay_status')
        this.payTypeDict = this.$options.filters['dictData']('pay_type')
        this.vipPackageDict = this.$options.filters['dictData']('vip_package')
        this.refundStatusDict = this.$options.filters['dictData']('refund_status')
      },
      /**
       * 高级搜索展开
       */
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      refund (item) {
        console.log(JSON.stringify(item))
        appVipOrderRefund(item).then((res) => {
          this.$refs.table.refresh(true)
        })
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }

  button {
    margin-right: 8px;
  }
</style>
